<template>
  <el-row :gutter="24" class="pl80">
    <el-col>
      <h2>
        培训信息
        <!-- <el-button size="small" @click="handleShowHistory">历史信息</el-button> -->
        <el-button type="primary" size="small" @click="handleEdit(false, {})" icon="el-icon-plus" />
      </h2>
    </el-col>
    <el-col>
      <e-table
        ref="eTable"
        @getList="handleGetList"
        :tableData="tableData"
        :columns="columns"
        :loading="loading"
        :pageObj="null"
      >
        <el-table-column slot="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>修改</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </e-table>
    </el-col>
    <train-modal ref="trainModal" @refresh="handleRefresh" />
  </el-row>
</template>

<script>
import { getUserTrain, deleteUserTrain } from '@/api/archives'
import { setSession, getSession, openNotification } from '@/utils/util'

import TrainModal from './TrainModal'

export default {
  components: {
    TrainModal
  },
  data() {
    return {
      loading: false,
      tableData: [],
      columns: [
        {
          label: '培训名称',
          prop: 'name'
        },
        {
          label: '培训开始时间',
          prop: 'startDate'
        },
        {
          label: '培训结束时间',
          prop: 'endDate'
        },
        {
          label: '培训学时',
          prop: 'period'
        },
        {
          label: '培训内容',
          prop: 'content'
        },
        {
          label: '培训成绩',
          prop: 'grade'
        },
        {
          label: '培训费用',
          prop: 'cost'
        },
        { slot: 'action' }
      ],
      userRow: null
    }
  },
  methods: {
    handleRefresh() {
      this.handleGetList(this.userRow.userId)
    },
    handleShowHistory() {
      this.$emit('showHistory', 'train')
    },
    handleEdit(bool, row) {
      this.$nextTick(() => {
        this.$refs.trainModal.edit(bool, row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteUserTrain(row.userTrainId, {})
          openNotification('success', '删除成功')
          _this.handleGetList(_this.userRow.userId)
        }
      })
    },
    async handleGetList(id, param = {}) {
      this.loading = true
      // this.queryParam = Object.assign(this.queryParam, param)

      const res = await getUserTrain(id, param)
      if (res && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
    if (this.userRow) {
      setSession('userRow', this.userRow)
    }
    this.handleGetList(this.userRow.userId)
  }
}
</script>
